import React from 'react';
import PropTypes from 'prop-types';
import {Card, Button, Row, Col, Table} from 'antd';
import {DATE_FORMAT, FILES_API, TABLE} from '../constants';
import DescriptionList from 'ant-design-pro/lib/DescriptionList';
import {PERMISSIONS} from './constants';
import {hasPermissions} from './utils';
import routes from './routes';
import {injectIntl} from './intl';
import {commerceInvoiceStatus, currency, packingType, packingListType} from '../Enum';
import {conventEnumValueToString, formatDateTime, formatAmount} from '../utils';
import styles from './style.css';
import {getDetail, abandonOrder} from './api';
import WrappedPopconfirm from '../common/WrappedPopconfirm';
const {Description} = DescriptionList;
export class DetailPanel extends React.PureComponent {
    state = {
        loading: false,
        data: {},
        details: [],
    }
    componentDidMount() {
        this.getDetailInfo();
    }
    getDetailInfo = () => {
        const id = this.props.id;
        this.setState({loading: true});
        getDetail(id)
            .then(res => {
                if(res.ok)
                    this.setState({
                        loading: false,
                        data: res.data,
                        details: res.data.details
                    });
                else
                    this.setState(({
                        loading: false
                    }));
            });
    }
    handleUpdateClick = () => {
        this.props.history.push(routes.update.format(this.props.id));
    }
    handleConfirmClick = () => {
        this.props.history.push(routes.confirm.format(this.props.id));
    }
    onClickAbandonBtn = () => {
        this.setState({loading: true});
        abandonOrder(this.state.data.id, this.state.data.rowVersion)
            .then(res => {
                this.setState({loading: false});
                if(res.ok)
                    this.props.successReturn();
            });
    }
    render() {
        const {formatMessage} = this.props.intl;
        const {data, details} = this.state;
        const operations = [];
        if(hasPermissions(this.props.permissions, PERMISSIONS.update) && hasPermissions(data.options, PERMISSIONS.update))
            operations.push(<Button key="update" onClick={this.handleUpdateClick} size="small">{
                formatMessage({
                    id: 'detailPage.btn.update',
                    defaultMessage: '编辑'
                })
            }</Button>);
        if(hasPermissions(this.props.permissions, PERMISSIONS.confirm) && hasPermissions(data.options, PERMISSIONS.confirm))
            operations.push(<Button key="confirm" onClick={this.handleConfirmClick} size="small">{
                formatMessage({
                    id: 'detailPage.btn.confirm',
                    defaultMessage: '确认'
                })
            }</Button>);
        if(hasPermissions(this.props.permissions, PERMISSIONS.abandon) && hasPermissions(data.options, PERMISSIONS.abandon))
            operations.push(<WrappedPopconfirm
                key="abandon"
                id={this.props.id}
                placement="topLeft"
                onConfirm={this.onClickAbandonBtn}
                title={formatMessage({
                    id: 'detailPanel.validate.abandon',
                    defaultMessage: '是否确认作废？'
                })}>
                <Button key="abandon" size="small">{
                    formatMessage({
                        id: 'detailPanel.btn.abandon',
                        defaultMessage: '作废'
                    })
                }</Button>
            </WrappedPopconfirm>);
        const columns = [{
            title: formatMessage({
                id: 'detailPanel.column.code',
                defaultMessage: '包装箱单号'
            }),
            dataIndex: 'code',
        }, {
            title: formatMessage({
                id: 'detailPanel.column.dealerCode',
                defaultMessage: '总代编号'
            }),
            dataIndex: 'dealerCode',
        }, {
            title: formatMessage({
                id: 'detailPanel.column.dealerName',
                defaultMessage: '总代名称'
            }),
            dataIndex: 'dealerName',
        }, {
            title: formatMessage({
                id: 'detailPanel.column.saleConfirmationrCode',
                defaultMessage: '销售确认书编号'
            }),
            dataIndex: 'saleConfirmationrCode',
        }, {
            title: formatMessage({
                id: 'detailPanel.column.batchNumber',
                defaultMessage: '批次号'
            }),
            dataIndex: 'batchNumber',
        }, {
            title: formatMessage({
                id: 'detailPanel.column.packingType',
                defaultMessage: '包装类型'
            }),
            dataIndex: 'packingType',
            render: text => conventEnumValueToString(packingType, text)
        }, {
            title: formatMessage({
                id: 'detailPanel.column.container',
                defaultMessage: '包装规格'
            }),
            dataIndex: 'container',
        }, {
            title: formatMessage({
                id: 'detailPanel.column.status',
                defaultMessage: '状态'
            }),
            dataIndex: 'status',
            render: text => conventEnumValueToString(packingListType, text)
        }, {
            title: formatMessage({
                id: 'detailPanel.column.length',
                defaultMessage: '长度(mm)'
            }),
            dataIndex: 'length',
        }, {
            title: formatMessage({
                id: 'detailPanel.column.width',
                defaultMessage: '宽度(mm)'
            }),
            dataIndex: 'width',
        }, {
            title: formatMessage({
                id: 'detailPanel.column.height',
                defaultMessage: '高度(mm)'
            }),
            dataIndex: 'height',
        }, {
            title: formatMessage({
                id: 'detailPanel.column.volume',
                defaultMessage: '体积(m³)'
            }),
            dataIndex: 'volume',
        }, {
            title: formatMessage({
                id: 'detailPanel.column.netWeight',
                defaultMessage: '净重(kg)'
            }),
            dataIndex: 'netWeight',
        }, {
            title: formatMessage({
                id: 'detailPanel.column.grossWeight',
                defaultMessage: '毛重(kg)'
            }),
            dataIndex: 'grossWeight',
        }, {
            title: formatMessage({
                id: 'detailPanel.column.createTime',
                defaultMessage: '导入时间'
            }),
            dataIndex: 'createTime',
            render: text => formatDateTime(text, DATE_FORMAT)
        }];
        return (
            <div className="detail">
                <Card loading={this.state.loading}>
                    <DescriptionList size="large" title={<Row><Col span={6}>{formatMessage({
                        id: 'detailPanel.text.title',
                        defaultMessage: 'CI详情'
                    })}</Col>
                    <Col className={`col-align-right ${styles.detail}`} span={18}>{operations}</Col>
                    </Row>}>
                        <Description term={formatMessage({
                            id: 'detailPanel.text.code',
                            defaultMessage: '发票号'
                        })}>{data.code}</Description>
                        <Description term={formatMessage({
                            id: 'detailPanel.text.brandName',
                            defaultMessage: '品牌'
                        })}>{data.brandName}</Description>
                        <Description term={formatMessage({
                            id: 'detailPanel.text.dealer',
                            defaultMessage: '总代	'
                        })}>{`(${data.dealerCode})${data.dealerName}`}</Description>
                        <Description term={formatMessage({
                            id: 'detailPanel.text.salePackingListCode',
                            defaultMessage: '包装箱单编号'
                        })}>{data.salePackingListCode}</Description>
                        <Description term={formatMessage({
                            id: 'detailPanel.text.currency',
                            defaultMessage: '币种'
                        })}>{conventEnumValueToString(currency, data.currency)}</Description>
                        <Description term={formatMessage({
                            id: 'detailPanel.text.totalAmount',
                            defaultMessage: '总金额'
                        })}>{formatAmount(data.totalAmount)}</Description>
                        <Description term={formatMessage({
                            id: 'detailPanel.text.status',
                            defaultMessage: '状态'
                        })}>{conventEnumValueToString(commerceInvoiceStatus, data.status)}</Description>
                        <Description term={formatMessage({
                            id: 'detailPanel.text.remark',
                            defaultMessage: '备注'
                        })}>{data.remark}</Description>
                        <Description term={formatMessage({
                            id: 'detailPanel.text.creatorName',
                            defaultMessage: '创建人'
                        })}>{data.creatorName}</Description>
                        <Description term={formatMessage({
                            id: 'detailPanel.text.createTime',
                            defaultMessage: '创建时间'
                        })}>{formatDateTime(data.createTime, DATE_FORMAT)}</Description>
                        <Description term={formatMessage({
                            id: 'detailPanel.text.modifierName',
                            defaultMessage: '修改人'
                        })}>{data.modifierName}</Description>
                        <Description term={formatMessage({
                            id: 'detailPanel.text.modifyTime',
                            defaultMessage: '修改时间'
                        })}>{formatDateTime(data.modifyTime, DATE_FORMAT)}</Description>
                        <Description term={formatMessage({
                            id: 'detailPanel.text.confirmorName',
                            defaultMessage: '确认人'
                        })}>{data.confirmorName}</Description>
                        <Description term={formatMessage({
                            id: 'detailPanel.text.confirmTime',
                            defaultMessage: '确认时间'
                        })}>{formatDateTime(data.confirmTime, DATE_FORMAT)}</Description>
                        <Description term={formatMessage({
                            id: 'detailPanel.text.abandonerName',
                            defaultMessage: '作废人姓名'
                        })}>{data.abandonerName}</Description>
                        <Description term={formatMessage({
                            id: 'detailPanel.text.abandonTime',
                            defaultMessage: '作废时间'
                        })}>{formatDateTime(data.abandonTime, DATE_FORMAT)}</Description>
                    </DescriptionList>
                </Card>
                <Card>
                    {data.attachments && data.attachments.length >= 0
                        ? <DescriptionList title={formatMessage({
                            id: 'detailPanel.card.attachment',
                            defaultMessage: '附件'
                        })}>
                            {data.attachments.map(item => (<Description key={item.fileId}>
                                <a icon="download" download href={`${FILES_API.files}/${item.fileId}`} size="small">
                                    {item.fileName}</a></Description>))}
                        </DescriptionList>
                        : <DescriptionList title={formatMessage({
                            id: 'detailPanel.content.noAttachment',
                            defaultMessage: '暂无附件'
                        })}>
                        </DescriptionList>}
                </Card>
                <Card>
                    <DescriptionList title={formatMessage({
                        id: 'detailPanel.table.details',
                        defaultMessage: '清单'
                    })}>
                        <Table
                            className="white-space-nowrap"
                            columns={columns}
                            rowKey="id"
                            pagination={false}
                            dataSource={details}
                            {...TABLE} />
                    </DescriptionList>
                </Card>
                <div className="page-toolbar">
                    <Button
                        type="primary"
                        disabled={this.state.loading}
                        onClick={this.props.onCancel}>{formatMessage({
                            id: 'detailPage.text.return',
                            defaultMessage: '返回'
                        })}</Button>
                </div>
            </div>
        );
    }
}

DetailPanel.propTypes = {
    id: PropTypes.string.isRequired,
    permissions: PropTypes.array.isRequired,
    onCancel: PropTypes.func.isRequired,
    history: PropTypes.object,
    intl: PropTypes.object,
    successReturn: PropTypes.func,
};

import {connect} from 'react-redux';
import {
    tableSearch
} from './actions';
import selectorFactory from 'Shared/utils/immutableToJsSelectorFactory';
const getPermissions = selectorFactory(['page', 'domainData', 'permission', 'data']);


const mapStateToProps = (state, props) => ({
    id: props.id,
    permissions: getPermissions(state),
});
const mapDispatchToProps = (dispatch, props) => ({
    successReturn: () => {
        dispatch(tableSearch());
        props.history.push(routes.query.url());
    },
    onCancel: () => {
        props.history.push(routes.query.url());
    },
});
export default connect(mapStateToProps, mapDispatchToProps)(injectIntl(DetailPanel));
